<!--用车申请页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">申请单列表</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="addApplicationDialogVisible=true">申请用车</el-button>
  </div>
  <!-- 申请用车弹窗 -->
  <el-dialog title="创建申请单" v-model="addApplicationDialogVisible" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用车人" >
            <el-input readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用时间">
            <el-date-picker
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="YYYY-MM-DD HH:mm:ss"
                format="YYYY-MM-DD HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆出发地">
            <el-input placeholder="请输入" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆目的地">
            <el-input placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用车事由">
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="3"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注">
            <el-input placeholder="请输入" type="textarea" resize="none" :rows="3"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="驾照">
            <el-upload
                v-model:file-list="fileList"
                v-model:action="actionUrl"
                name="file"
                limit="1"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
            >
              <el-icon>
                <Plus/>
              </el-icon>
            </el-upload>
            <el-dialog v-model="dialogVisible">
              <img w-full :src="dialogImageUrl"/>
            </el-dialog>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审批人">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-tag type="primary">
                  <el-icon style="margin-right:5px;position:relative;top:2px;">
                    <User/>
                  </el-icon>
                  <span>审批人姓名1</span>
                </el-tag>
              </el-col>
              <el-col :span="12">
                <el-tag type="primary">
                  <el-icon style="margin-right:5px;position:relative;top:2px;">
                    <User/>
                  </el-icon>
                  <span>审批人姓名2</span>
                </el-tag>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button>取消</el-button>
      <el-button type="primary">确定</el-button>
    </template>
  </el-dialog>

  <!-- 用车申请列表 搜索卡片 -->
  <el-card style="margin: 20px;">
    <el-form style="padding-top:10px;">
      <el-row :gutter="30">
        <el-col :span="5">
          <el-form-item label="出发地">
            <el-input placeholder="请输入出发地" v-model="Application.departureAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="目的地">
            <el-input placeholder="请输入目的地" v-model="Application.destinationAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5" >
          <el-form-item label="申请单状态">
            <el-select placeholder="请选择" v-model="Application.status">
              <el-option label="已发起" value="10"></el-option>
              <el-option label="撤销" value="20"></el-option>
              <el-option label="审核中" value="30"></el-option>
              <el-option label="驳回" value="40"></el-option>
              <el-option label="已通过" value="50"></el-option>
              <el-option label="分配用车" value="60"></el-option>
              <el-option label="工单结束" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <el-button>重置</el-button>
            <el-button type="primary" @click="selectApplication" @change="selectApplication">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <!-- 用车申请表格 -->
  <el-card style="margin:20px" >
    <el-table :data="applicationList">
      <el-table-column type="index" width="80" align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="username" label="申请人"></el-table-column>
      <el-table-column align="center" prop="departureAddr" label="出发地"></el-table-column>
      <el-table-column align="center" prop="destinationAddr" label="目的地"></el-table-column>
      <el-table-column align="center" prop="reason" label="用车原因"></el-table-column>
      <el-table-column align="center" prop="auditUsernameList" label="审批人"></el-table-column>
      <el-table-column align="center" prop="startTime" label="使用开始时间"></el-table-column>
      <el-table-column align="center" prop="endTime" label="使用结束时间"></el-table-column>
      <el-table-column align="center" prop="status" label="申请单状态"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link>撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from 'qs';
import {ElMessage} from "element-plus";


//获取当前登录人数据
const user = ref(getUser());
//定义数组用来保存新增申请单弹窗中的多个审批人
const auditUserOpts =ref([]);
//定义数组用来保存多个审批人id，有几个id就代表有几个审批人，就要生成对应的几条审批单数据
const auditUserIdList =ref([]);
// //定义加载审批人的方法
// const loadAuditUser =()=>{
//   //获取当前登录人的直属领导id
//   axios.get(BASE_URL+'/v1/user/select/audit/'+parentId).then((response)=>{
//     if(response.data.code==2000){
//       auditUserOpts.value = response.data.data;
//       //依次遍历取出每个审批人的id，方便后续生成对应的审批单数据
//       for (let i =0;i<auditUserOpts.value.length;i++){
//         auditUserIdList.value.push(auditUserOpts.value[i].id);
//       }
//       }else {
//         ElMessage.error(response.data.message);
//       }
//     }
//   )
// }
// onMounted(()=>{
//   loadAuditUser();
// })
//定义变量控制创建申请单弹窗是否出现
const addApplicationDialogVisible = ref(false);

const times =ref([]);
//定义弹窗的表格的数据
const addForm = ref({
  username:'',
  destinationAddf:'',
  reason:'',
  remark:'',
})
//定义新增申请单的方法
const addApplication =()=>{
  //对图片进行非空校验
  if (fileList.value.length ==0 ){
    ElMessage.error("请上传图片！")
    return;
  }
  addform.value.userId = user.value.id;
  addform.value.username = user.value.username;
  addform.value.startTime = times.value[0];
  addform.value.endTime =times.value[1];
  addform.value.imgUrl = fileList.value[0].response.data;
  addform.value.auditUserIdList = auditUserIdList.value;
  console.log(addForm.value);
}

/**图片上传相关代码开始**/
const fileList = ref([]);
const dialogImageUrl = ref('');
const dialogVisible = ref(false);
//定义变量用来保存图片上传给哪个后端接口地址
const actionUrl = ref(BASE_URL + '/v1/file/upload');
//移除图片
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
  //1.得到要删除的图片路径 uploadFile就是我们要删除的那个图片文件对象
  let imgUrl = uploadFile.response.data;
  //2.给后端发请求,删除图片
  axios.post(BASE_URL+'/v1/file/remove?imgUrl='+imgUrl)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success('删除成功!');
        }
      })
}
//图片上传后预览
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  dialogVisible.value = true;
  console.log(uploadFile);
  //console.log(fileList.value[0].response.data);
}
/**图片上传相关代码结束**/

/*申请查询*/
//定义变量用来保存查询条件
const Application =ref({
  departureAddr:'',
  destinationAddr:'',
  status:''
});
//装载前端返回的查询结果
const applicationList = ref([]);
const selectApplication =()=>{
  console.log('前段查询条件{}',Application.value);
  let data = qs.stringify(Application.value);
  axios.get(BASE_URL+'/v1/application/select?'+data)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success('查询成功!');
          applicationList.value = response.data.data;
        }else {
          ElMessage.error('查询失败!');
        }
      })
};
onMounted(()=>{
  selectApplication();
})
// //定义重置搜索的方法
// const resetApplication =()=>{
//   searchApplication.value ={};
//   loadApplication();
// }
// onMounted(()=>{
//   loadApplication();
// })
// //定义撤销申请用车的方法
// const cancel =(id)=>{
//   axios.get(BASE_URL+'/v1/application/cancel/'+id).then((response)=>{
//     if (response.data.code==2000){
//       ElMessage.success('撤销成功');
//       loadApplication();
//     }else {
//       ElMessage.error('撤销失败');
//     }
//   })
// }
</script>

<style scoped>
</style>